home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 1639 < prev    next >
Encoding:
Text File  |  1996-08-05  |  1.7 KB  |  86 lines

  1. Path: su3.in.net!news
  2. From: poundss@in.net (Sam Pounds)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Help with simple code
  5. Date: Mon, 15 Jan 1996 23:49:13 GMT
  6. Organization: INTERNET Indiana
  7. Message-ID: <4deocm$4co@su3.in.net>
  8. References: <4dbak5$oij@ionews.io.org>
  9. NNTP-Posting-Host: pm4-01.in.net
  10. X-Newsreader: Forte Free Agent 1.0.82
  11.  
  12. jgordon@io.org (John Gordon MacPherson) wrote:
  13.  
  14. >Can anyone tell me what's wrong with this piece of code? I lifted it
  15. >straight from a textbook.
  16.  
  17. >Here's the code:
  18.  
  19. >/* Calculating compound interest */
  20. >#include <stdio.h>
  21. >#include <math.h>
  22.  
  23. >main()
  24. >{
  25. >    int year;
  26. >    double amount, principal = 1000, rate = 0.5;
  27.  
  28. >    printf("%4s%21s\n", "Year", "Amount on deposit");
  29.  
  30. >    for (year = 1; year <= 10; year++) {
  31. >        amount = principal * pow(1.0 + rate, year);
  32. >        printf("%4d%21.2f\n", year, amount);
  33. >    }
  34.  
  35. >    return 0;
  36. >}
  37. >______________________________________________________________
  38.  
  39. >and here's the error:
  40.  
  41. >In function `main':
  42. >undefined reference to `pow'
  43.  
  44. >I don't understand. `pow' is a function, not a variable?
  45. >Can anyone tell me what's wrong?
  46.  
  47. Make sure you have the correct "path"  for "math.h"
  48. I think THAT is your problem.
  49.  
  50. Below is a "TEST"power  function.
  51.  
  52.  
  53. /* Calculating compound interest */
  54. #include <stdio.h>
  55.  
  56. double power(double base, double n);
  57.  
  58. int main()
  59. {
  60.     int year;
  61.     double amount, principal = 1000, rate = 0.5;
  62.  
  63.     printf("%4s%21s\n", "Year", "Amount on deposit");
  64.  
  65.     for (year = 1; year <= 10; year++) {
  66.       /*    amount = principal * pow(1.0 + rate, year); */
  67.     amount = principal * power(1.0 + rate, year);
  68.         printf("%4d%21.2f\n", year, amount);
  69.     }
  70.  
  71.     return 0;
  72. }
  73.  
  74. double power(double base, double n)
  75. {
  76.     int i;
  77.     double p;
  78.  
  79.     p = 1;
  80.     for (i = 1; i <= n; ++i)
  81.         p = p * base;
  82.     return p;
  83. }
  84.  
  85.  
  86.